const url = require('url');
const path = require('path');
const fs = require('fs');
const querystring = require("querystring");

function resSend(res) {
    res.send_tah = (data) => {
        res.writeHead(200, { 'Content-Type': 'text/html;charset="utf-8"' });
        res.end(data);

        // console.log(data);
    }
}

//根据后缀名获取文件类型
function getFileMime(extname) {
    var data = fs.readFileSync('./data/mime.json')
    let mimeObj = JSON.parse(data.toString());
    return mimeObj[extname];
}

function initStatic(req, res, staticPath) {

    // 1.获取地址

    let pathname = url.parse(req.url).pathname;
    // console.log(1,pathname);
    pathname = decodeURIComponent(pathname);
    // console.log(2,pathname);
    pathname = pathname == '/' ? '/index.html' : pathname;
    let extname = path.extname(pathname)
    // console.log(3,extname);
    // 可以获取后缀名path.extname() 

    // 2.通过fs读取文件
    try {
        let data = fs.readFileSync('./' + staticPath + pathname);
        if (data) {
            let mime = getFileMime(extname);
            res.writeHead(200, { 'Content-Type': '' + mime + ';charset="utf-8"' });
            res.end(data);
        }
    } catch (error) {
        console.log(error);
    }
    console.log("static path has finished!!");
}

let server = () => {
    var pass=0;
    var password=JSON.parse(fs.readFileSync('./data/admin_passwords.json'));
    console.log(password);
    var adm={
        "admin":"127.0.0.1",
        "tmpadmin":""
    }
    console.log("admin:"+adm.admin);
    console.log("tmpadmin:"+adm.tmpadmin);
    console.log("init")

    let G = {
        _get: {},
        _post: {},
        staticPath: 'static' //默认静态web目录
    };
    
    let app = function (req, res) {
        resSend(res);
        initStatic(req, res, G.staticPath);
        adm.tmpadmin=req.rawHeaders[1];
        console.log("admin:"+adm.admin);
        console.log("tmpadmin:"+adm.tmpadmin);
        if(adm.admin!=adm.tmpadmin){
            pass=0;
        }else{
            pass=1;
        }
        
        let pathname = url.parse(req.url).pathname;
        let method = req.method.toLowerCase();
        let query = url.parse(req.url).query;
        query = querystring.parse(query);
        // console.log(query);
        // console.log("req.url:" + req.url);
        // console.log(method);
        // console.log(pass);
        if (G['_' + method][pathname]) {
            if (method == 'get' && pass==1) {
                console.log("get path start!!");
                G._get[pathname](req, res, query);
            } else if (method == 'post' && pass==1) {
                console.log("post path start!!");
                G._post[pathname](req, res, query);
            } else if (method == 'post' && pathname=="/login" && pass==0) {
                console.log("login path start!!");
                G._post[pathname](req, res, password,adm);
            }else {
                console.log("pls login first!!");
                res.setHeader("Access-Control-Allow-Origin", "*");
                res.send_tah("pls login first!!");
            }
        }
    }

    app.get = (modulename, modulefuc) => {
        G._get[modulename] = modulefuc;
    }
    app.post = (modulename, modulefuc) => {
        G._post[modulename] = modulefuc;
    }
    return app;
}
module.exports = server();